package com.gopay.webtest.controller;

import java.io.IOException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.util.CookieHandle;
import com.gopay.webtest.xml.XmlDao;

/**
 * 参数设置
 * @author 曹永峰
 *
 */
@Controller
public class ParamSettingController {
	private String INDIVIDUALIRY = "自定义";
	private String KEY = "11111aaaaa";
	@Resource(name = "xmlDao")
	private XmlDao xmlDao;
	
	/**
	 * 展示默认的测试地址
	 * @return
	 */
	@RequestMapping("/showUrlList")
	public ModelAndView showUrlList() {
		ModelAndView mav = new ModelAndView();
		//读取url地址
		Map<String,String> devUrl = new HashMap<String,String>();
		try {
			Map map = xmlDao.readData4Map("url","url");
			Iterator it = map.keySet().iterator();
			while(it.hasNext()){
				String key = (String) it.next();
				if(key.equals("defualt")){
					continue;
				}
				String url = xmlDao.readEvnNode("url",key).get("dom");
				devUrl.put(key, url);
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		mav.addObject("urls",devUrl);
		
		mav.setViewName("gopay.webtest.setParm");
		return mav;
	}
	
	/**
	 * 设置测试地址
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/setParam")
	public ModelAndView setParam(HttpServletRequest request,
			   HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		String name =request.getParameter("key");
		//读取url地址
		Map<String,String> devUrl = null;
		try {
			Map map =xmlDao.readData4Map("url","url");
			devUrl = xmlDao.readEvnNode("url",name);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		request.getSession().setAttribute("gopay_webtest_URL", devUrl);
		request.getSession().setAttribute("gopay_webtest_URL_Name", name);
		mav.setViewName("gopay.webtest.paramSetSuccess");
		return mav;
	}
	/**
	 * 设置自定义测试地址及商户识别码
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/setIndividualityParam")
	public ModelAndView setIndividualityParam(HttpServletRequest request,
			   HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		String url =request.getParameter("url");
		//商户识别码
		String key =request.getParameter("key");
		String isWebtestRedirect = request.getParameter("isWebtestRedirect");
		if(key==null||key.equals("")){
			key=KEY;
		}
		if(url!=null&&!url.equals("")){
			Map<String,String> devUrl = null;
			try {
				Map map =xmlDao.readData4Map("url","url");
				String defualt = (String) map.get("defualt");
				devUrl = xmlDao.readEvnNode("url",defualt);
				devUrl.put("dom", url);
			} catch (DocumentException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			request.getSession().setAttribute("gopay_webtest_URL", devUrl);
			request.getSession().setAttribute("gopay_webtest_URL_Name", INDIVIDUALIRY);
		}
		request.getSession().setAttribute("gopay_webtest_key", key);
		request.getSession().setAttribute("isWebtestRedirect", isWebtestRedirect);
		mav.setViewName("gopay.webtest.paramSetSuccess");
		return mav;
	}
}
